package chapter7.shovel;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by uu on 2017/6/26.
 */
public class shovel_customer {

    public static void main(String[] args) {
        ConnectionFactory factory =  new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setHost("localhost");
        factory.setPort(5676);
            try {
                Connection conn = factory.newConnection();
                final Channel channel = conn.createChannel();
                channel.queueDeclare("warehouse_carpinteria",true,false,false,null);
                channel.exchangeDeclare("incoming_orders","direct",true,false,null);
                channel.queueBind("warehouse_carpinteria","incoming_orders","warehouse");
                System.out.println("Ready for orders");
                channel.basicConsume("warehouse_carpinteria",false, "order_processor",
                        new DefaultConsumer(channel) {
                            @Override
                            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                                String routingKey = envelope.getRoutingKey();
                                String contentType = properties.getContentType();
                                long deliveryTag = envelope.getDeliveryTag();
                                // (process the message components here ...)
                                System.out.println(new String(body, "UTF-8"));
                                channel.basicAck(deliveryTag, false);
                            }
                        });
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
    }
}
